#search engineer
class SearchController < ApplicationController
  
  def index
    redirect_to :controller=>"list",:action=>"index"
  end
  
  def method_missing(name, *args) 
    render :template=>"index/null"
  end
  
  def search
    query = params[:search][:query].strip
    category_id = params[:search][:category].to_i
    if category_id==5
      redirect_to :action =>"list_users", :search=>query
    elsif category_id==4
      redirect_to :action =>"list_topics", :search=>query
    elsif category_id==3
      redirect_to :action =>"list_companies", :search=>query
    elsif category_id==2
      redirect_to :action =>"list_reports", :search=>query
    else
      redirect_to :action =>"list_incumbents", :search=>query 
    end
  end
  
  def search_keyword
    id=params[:id].to_i
    if searchword = Keyword::SearchWord.find_by_id(id)
      @query=searchword.word
      @query_arrays=[searchword.word]
      query_conditions=searchword.create_query_conditions(1,"deleted=0")
      @pages, @objects = paginate :Incumbents,:conditions => query_conditions,:per_page => 7,:order=>"view_times DESC"
    else
      @objects=[]
      @query=""
    end
    render :template=>"search/search"
  end
  
  def search_tagword
    id=params[:id].to_i
    category_id = params[:category].to_i
    if tagword = Keyword::TagWord.find_by_id(id)
      @query=tagword.word
      @query_arrays=[tagword.word]
      if category_id==5
        query_conditions=tagword.create_query_conditions(category_id,"deleted=0 and anonymous=0")
        @pages, @objects = paginate :Users,:conditions => query_conditions,:per_page => 7,:order=>"ol_time DESC"
      elsif category_id==4
        query_conditions=tagword.create_query_conditions(category_id)
        @pages, @objects = paginate :Topics,:conditions => query_conditions,:per_page => 7,:order=>"view_times DESC"
      elsif category_id==3
        query_conditions=tagword.create_query_conditions(category_id,"deleted=0")
        @pages, @objects = paginate :Companies,:conditions => query_conditions,:per_page => 7,:order=>"view_times DESC"
      elsif category_id==2
        query_conditions=tagword.create_query_conditions(category_id,"state=3 and user_id=0")
        @pages, @objects = paginate :Reports,:conditions => query_conditions,:per_page => 7,:order=>"view_times DESC"
      else
        query_conditions=tagword.create_query_conditions(1,"deleted=0")
        @pages, @objects = paginate :Incumbents,:conditions => query_conditions,:per_page => 7,:order=>"view_times DESC" 
      end
    else
      @objects=[]
      @query=""
    end
    render :template=>"search/search"
  end
  
  
  def list_incumbents
    @query = params[:search]||""
    @query_arrays,query_conditions = @activeuser.create_query_conditions(@query,1,"deleted=0")
    @pages, @objects = paginate :Incumbents,:conditions => query_conditions,:per_page => 7,:order=>"view_times DESC"
    render :template=>"search/search"
  end
  
  def list_reports
    @query = params[:search]||""
    if @activeuser.guest?
      @query_arrays,query_conditions=  @activeuser.create_query_conditions(@query,2,"state=3 and user_id=0")
    else
      @query_arrays,query_conditions=  @activeuser.create_query_conditions(@query,2,"state=3")
    end   
    @pages, @objects = paginate :Reports,:conditions => query_conditions,:per_page => 7,:order=>"view_times DESC"
    render :template=>"search/search"
  end
  
  def list_companies
    @query = params[:search]||""
    @query_arrays,query_conditions=  @activeuser.create_query_conditions(@query,3,"deleted=0")   
    @pages, @objects = paginate :Companies,:conditions => query_conditions,:per_page => 7,:order=>"view_times DESC"
    render :template=>"search/search"
  end
  
  def list_topics
    @query = params[:search]||""
    @query_arrays,query_conditions=  @activeuser.create_query_conditions(@query,4)   
    @pages, @objects = paginate :Topics,:conditions => query_conditions,:per_page => 7,:order=>"view_times DESC"
    render :template=>"search/search"
  end
  
  def list_users
    @query = params[:search]||""
    @query_arrays,query_conditions=  @activeuser.create_query_conditions(@query,5)   
    @pages, @objects = paginate :Users,:conditions => query_conditions,:per_page => 7,:order=>"ol_time DESC"
    render :template=>"search/search"
  end
  
  def listindustry
    id = params[:id] || -1
    @pages, @objects = paginate :Companies,:conditions=>["industry_id=? and deleted=0", id],:per_page => 7,:order=>"view_times DESC"
    render :template =>"/search/list_company"
  end
  
  def listincumbent
    @pages, @objects = paginate :Incumbents,:per_page => 10,:order=>"created_time DESC"
    render :template =>"/search/list_incumbent"
  end
  
  def listreport
    category = params[:category] || -1
    @pages, @objects = paginate :Reports,:conditions=>["category=? and state=3", category],:per_page => 7,:order=>"created_time DESC"
    render :template =>"/search/list_report"
  end
  
  def listposition
    id = params[:id] || -1
    @pages, @objects = paginate :Incumbents,:conditions=>["deleted=0 and position_id=?", id],:per_page => 7,:order=>"created_time DESC"
    render :template =>"/search/list_incumbent"
  end
  
  def listcity
    id = params[:id] || -1
    @pages, @objects = paginate :Incumbents,:conditions=>["deleted=0 and city_id=?", id],:per_page => 7,:order=>"created_time DESC"
    render :template =>"/search/list_incumbent"
  end
  
  def postcompany
    company_cname = params[:company_cname].strip
    addition="industry_id ="+ params[:company_industry_id] + " and entity_id ="+params[:company_entity_id]+" and deleted=0"
    @query_arrays, query_string=  SearchController.create_query_string(company_cname,3,addition)         
    @companies=Company.find(:all, 
                            :limit => 8,
                            :conditions=>query_string)
    render :partial => "report/companies_list"
  end
  
end
